/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.web.open.app.user;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.dto.DepartmentUserDto;
import com.jiantong.user.dto.OrganizationUserDto;
import com.jiantong.user.enums.DepartmentType;
import com.jiantong.user.enums.DepartmentUserType;
import com.jiantong.user.enums.OrganizationUserType;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.Department;
import com.jiantong.user.model.DepartmentUser;
import com.jiantong.user.model.OrganizationUser;
import com.jiantong.user.model.User;
import com.jiantong.user.service.DepartmentUserWriteService;
import com.jiantong.user.service.DepartmentWriteService;
import com.jiantong.user.service.OrganizationUserWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author : panxin
 */
@Slf4j
@OpenBean
public class GovernmentApis {

    @RpcConsumer
    private OrganizationUserWriteService organizationUserWriteService;
    @RpcConsumer
    private DepartmentWriteService departmentWriteService;
    @RpcConsumer
    private DepartmentUserWriteService departmentUserWriteService;

    /**
     * 创建部门
     *
     * @param dept 部门信息
     * @return 创建结果
     */
    @OpenMethod(key = "gov.create.department", paramNames = {"dept"}, httpMethods = RequestMethod.POST)
    public Boolean createDepartment(Department dept) {
        Long govId = loginUser().getOrganizationId();
        String govName = loginUser().getOrganizationName();
        Integer type = DepartmentType.NORMAL_DEPARTMENT.value();

        dept.setType(type);
        dept.setGovernmentId(govId);
        dept.setGovernmentName(govName);

        Response<Boolean> resp = departmentWriteService.createNormalDepartment(dept);
        if (!resp.isSuccess()) {
            log.error("failed to create department = {}, cause : {}", dept, resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 创建政府用户
     *
     * @param userDto 用户信息
     * @return 创建结果
     */
    @OpenMethod(key = "create.government.user", paramNames = {"userDto"}, httpMethods = RequestMethod.POST)
    public Boolean createGovernmentUser(OrganizationUserDto userDto) {
        if (userDto.getUser() == null || userDto.getOrganizationUser() == null) {
            log.error("user info is null, user dto = {}", userDto);
            throw new OPClientException("user.info.is.not.integral");
        }
        User user = userDto.getUser();
        OrganizationUser organizationUser = userDto.getOrganizationUser();

        checkUserArgument(user);
        checkOrganizationUserArgument(organizationUser);

        // 类型
        Integer roleType = organizationUser.getRoleType();
        organizationUser.setUserName(user.getName());
        organizationUser.setRoleName(OrganizationUserType.from(roleType).toString());

        Response<Boolean> resp = organizationUserWriteService.createGovernmentUser(user, organizationUser);
        if (!resp.isSuccess()) {
            log.error("failed to create government user, user = {}, organization user = {}, cause : {}",
                    user, organizationUser, resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 创建部门用户
     *
     * @param userDto 用户信息
     * @return 创建结果
     */
    @OpenMethod(key = "create.dept.user", paramNames = {"userDto"}, httpMethods = RequestMethod.POST)
    public Boolean createDepartmentUser(DepartmentUserDto userDto) {
        if (userDto.getUser() == null || userDto.getDepartmentUser() == null) {
            log.error("user info is null, user dto = {}", userDto);
            throw new OPClientException("user.info.is.not.integral");
        }
        User user = userDto.getUser();
        DepartmentUser departmentUser = userDto.getDepartmentUser();

        checkUserArgument(user);
        checkDepartmentUserArgument(departmentUser);

        // 类型
        Integer roleType = departmentUser.getEmpType();
        departmentUser.setUserName(user.getName());
        departmentUser.setEmpType(DepartmentUserType.from(roleType).value());

        Response<Boolean> resp = departmentUserWriteService.createNormalDepartmentUser(user, departmentUser);
        if (!resp.isSuccess()) {
            log.error("failed to create department user, user = {}, department user = {}, cause : {}",
                    user, departmentUser, resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }

    private void checkDepartmentUserArgument(DepartmentUser departmentUser) {
        if (departmentUser.getEmpType() == null) {
            throw new OPClientException("department.user.emp.type.is.null");
        }
    }

    private void checkOrganizationUserArgument(OrganizationUser organizationUser) {
        if (organizationUser.getRoleType() == null) {
            throw new OPClientException("organization.user.role.type.is.null");
        }
    }

    private void checkUserArgument(User user) {
        if (Strings.isNullOrEmpty(user.getName())) {
            throw new OPClientException("user.name.is.null");
        }
        if (Strings.isNullOrEmpty(user.getMobile())) {
            throw new OPClientException("user.mobile.is.null");
        }
    }

    private LoginUser loginUser() {
        return UserUtil.<LoginUser>getCurrentUser();
    }

}
